﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using global::TerhelesEloszto.Common;
using global::TerhelesEloszto.Engine;

namespace Presentation
{
	public partial class CreateTask : System.Web.UI.Page
	{
		protected global::TerhelesEloszto.Engine.IEngine engine = global::TerhelesEloszto.Engine.Engine.Instance();
		protected void Page_Load( object sender, EventArgs e )
		{
			String visitorName = User.Identity.Name;
			if( !engine.IsAdmin( visitorName ) )
			{
				if( engine.IsTeacher( visitorName ) )
				{
					Response.Redirect( "~/Teacher/TeacherProblem.aspx" );
				}
				else
				{
					Response.Redirect( "~/Problem.aspx" );
				}

			}
			tasktype.AutoPostBack = true;
			try
			{
				if( !Page.IsPostBack )
				{
					loadFacts();
					loadTypes();
					loadSkills();
				}
			}

			catch( EngineException ex )
			{
				Response.Redirect( "~/Admin/Task.aspx?msg=0" );
			}
		}
		protected void loadFacts()
		{
			foreach( IFact actFact in engine.GetFacts() )
			{
				ListItem item = new ListItem();
				item.Text = actFact.Name + "(" + actFact.Neptun + ")";
				FactList.Items.Add( item );
			}
			//FactList.SelectedIndex = 0;

		}

		protected void loadTypes()
		{
			foreach( ITaskType actType in engine.GetTaskTypes() )
			{
				ListItem item = new ListItem();
				item.Text = actType.Name;
				tasktype.Items.Add( item );

			}
			//tasktype.SelectedIndex = 0;

		}

		protected void loadSkills()
		{
			foreach( ISkill actSkill in engine.GetSkills() )
			{
				ListItem item = new ListItem();
				item.Text = actSkill.Name;
				SkillList.Items.Add( item );
			}
			//SkillList.SelectedIndex = 0;

		}

		protected void submit_Click( object sender, EventArgs e )
		{
			SemesterType semester = SemesterType.autumn;
			if( SemesterList.SelectedValue == "Őszi" )
			{
				semester = SemesterType.autumn;
			}
			else if( SemesterList.SelectedValue == "Tavaszi" )
			{
				semester = SemesterType.spring;
			}
			else if( SemesterList.SelectedValue == "Mindkettő" )
			{
				semester = SemesterType.both;
			}
			if( Vname.IsValid && Vskill.IsValid )
			{
				ITaskType ttype = engine.GetTaskTypes()[tasktype.SelectedIndex];
				IFact fact = engine.GetFacts()[FactList.SelectedIndex];


				List<ISkill> skills = new List<ISkill>();
				foreach( int index in SkillList.GetSelectedIndices() )
				{
					skills.Add( engine.GetSkills()[index] );
				}
				try
				{
					engine.CreateTask( fact, Name.Text, ttype, skills, Convert.ToInt32( HoursList.SelectedValue ), semester );

					Response.Redirect( "~/Admin/Task.aspx?msg=1" );
				}
				catch( EngineException ex )
				{
					Response.Redirect( "~/Admin/Task.aspx?msg=0" );
				}

			}

		}




		protected void tasktype_SelectedIndexChanged1( object sender, EventArgs e )
		{
			ITaskType ttype = engine.GetTaskTypes()[tasktype.SelectedIndex];
			if( ttype.HasInterval )
			{
				HoursList.Enabled = false;
			}
			else
			{
				HoursList.Enabled = true;
			}
		}


	}
}